#!/usr/bin/perl
#                              -*- Mode: Cperl -*- 
# debian.postinst ---
# Author           : Manoj Srivastava ( srivasta@pilgrim.umass.edu )
# Created On       : Sat Apr 27 05:42:43 1996
# Created On Node  : melkor.pilgrim.umass.edu
# Last Modified By : Manoj Srivastava
# Last Modified On : Mon Apr 13 14:24:56 2009
# Last Machine Used: anzu.internal.golden-gryphon.com
# Update Count     : 44
# Status           : Unknown, Use with caution!
# HISTORY          :
# Description      :
#
#
#
#  arch-tag: ae5907e4-2617-4db5-964a-1b5f45bedc1a
#


$|=1;

# Predefined values:
my $package="=P";
my $version="=V";

my $kernel_pkg_version = "=KPV";

# Ignore all invocations uxcept when called on to configure.
exit 0 unless ($ARGV[0] && $ARGV[0] =~ /configure/);

my $architecture;
chomp($architecture = `dpkg --print-installation-architecture`);
$architecture = "ppc" if $architecture eq "powerpc";
$architecture = "parisc" if $architecture eq "hppa";
$architecture = "mips" if $architecture eq "mipsel";
$architecture = "x86_64" if $architecture eq "amd64";

my $stop_and_read     = 0;
my $have_conffile     = "";
my $src_postinst_hook = '';
my $CONF_LOC          = '/etc/kernel-img.conf';

# most of our work is done in /usr/src.
chdir '/usr/src' or die "Could not chdir to /usr/src:$!";

if (-r "$CONF_LOC" && -f "$CONF_LOC"  ) {
  if (open(CONF, "$CONF_LOC")) {
    while (<CONF>) {
      chomp;
      s/\#.*$//g;
      next if /^\s*$/;

      $src_postinst_hook   = "$1"  if /src_postinst_hook\s*=\s*(\S+)/ig;
    }
    close CONF;
    $have_conffile = "Yes";
  }
}

sub exec_script {
  my $type   = shift;
  my $script = shift;
  print STDERR "Running $type hook script $script.\n";
  system ("$script $version $realimageloc$kimage-$version") &&
    print STDERR "User $type hook script [$script] ";
  if ($?) {
    if ($? == -1) {
      print STDERR "failed to execute: $!\n";
    }
    elsif ($? & 127) {
      printf STDERR "died with signal %d, %s coredump\n",
        ($? & 127),  ($? & 128) ? 'with' : 'without';
    }
    else {
      printf STDERR "exited with value %d\n", $? >> 8;
    }
    exit $? >> 8;
  }
}
sub run_hook {
  my $type   = shift;
  my $script = shift;
  if ($script =~ m,^/,) {
    # Full path provided for the hook script
    if (-x "$script") {
      &exec_script($type,$script);
    }
    else {
      die "The provided $type hook script [$script] could not be run.\n";
    }
  }
  else {
    # Look for it in a safe path
    for my $path ('/bin', '/sbin', '/usr/bin', '/usr/sbin') {
      if (-x "$path/$script") {
        &exec_script($type, "$path/$script");
        return 0;
      }
    }
    # No luck
    print STDERR "Could not find $type hook script [$script].\n";
    die "Looked in: '/bin', '/sbin', '/usr/bin', '/usr/sbin'\n";
  }
}

# Set up the env variable containing our arguments
my $out;
for (@ARGV) {
  s,','\\'',g;
  $out.=" '$_'";
}
$ENV{'DEB_MAINT_PARAMS'}="$out";
$ENV{'KERNEL_PACKAGE_VERSION'}="$kernel_pkg_version";

## Run user hook script here, if any
if (-d "/etc/kernel/src_postinst.d") {
  print STDERR "Examining /etc/kernel/src_postinst.d.\n";
  system ("run-parts --verbose --exit-on-error --arg=$version " .
          "--arg=$realimageloc$kimage-$version " .
          "/etc/kernel/src_postinst.d") &&
            die "Failed to process /etc/kernel/src_postinst.d";
}

if (-d "/etc/kernel/src_postinst.d/$version") {
  print STDERR "Examining /etc/kernel/src_postinst.d/$version.\n";
  system ("run-parts --verbose --exit-on-error --arg=$version " .
          "--arg=$realimageloc$kimage-$version " .
          "/etc/kernel/src_postinst.d/$version") &&
            die "Failed to process /etc/kernel/src_postinst.d/$version";
}

if (-x "$src_postinst_hook") {
  &run_hook("postinst", $src_postinst_hook);
}


exit 0;

__END__








